﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace ParticlePlayground
{
	// Token: 0x02000137 RID: 311
	public class PlaygroundFollow : MonoBehaviour
	{
		// Token: 0x1400000D RID: 13
		// (add) Token: 0x06000670 RID: 1648 RVA: 0x000266E8 File Offset: 0x000248E8
		// (remove) Token: 0x06000671 RID: 1649 RVA: 0x00026704 File Offset: 0x00024904
		public event OnPlaygroundFollower followerEventBirth;

		// Token: 0x1400000E RID: 14
		// (add) Token: 0x06000672 RID: 1650 RVA: 0x00026720 File Offset: 0x00024920
		// (remove) Token: 0x06000673 RID: 1651 RVA: 0x0002673C File Offset: 0x0002493C
		public event OnPlaygroundFollower followerEventDeath;

		// Token: 0x06000674 RID: 1652 RVA: 0x00026758 File Offset: 0x00024958
		public virtual void Start()
		{
			if (this.referenceObject == null || this.particles == null)
			{
				return;
			}
			this.birthEvent = PlaygroundC.CreateEvent(this.particles);
			this.birthEvent.broadcastType = EVENTBROADCASTC.EventListeners;
			this.birthEvent.eventType = EVENTTYPEC.Birth;
			this.deathEvent = PlaygroundC.CreateEvent(this.particles);
			this.deathEvent.broadcastType = EVENTBROADCASTC.EventListeners;
			this.deathEvent.eventType = EVENTTYPEC.Death;
			this.birthEvent.particleEvent += this.OnParticleDidBirth;
			this.deathEvent.particleEvent += this.OnParticleDidDie;
			this.followerParent = new GameObject("Followers").transform;
			this.followerParent.parent = base.transform;
			this.referenceTrailRenderer = this.referenceObject.GetComponent<TrailRenderer>();
			if (this.referenceTrailRenderer != null)
			{
				this.trailTime = this.referenceTrailRenderer.time;
			}
			int num = (this.followerLifetime > 0f) ? (Mathf.CeilToInt(Mathf.Abs(this.particles.lifetime - this.followerLifetime) + (this.followerLifetime - this.particles.lifetime)) + 2) : (Mathf.CeilToInt(Mathf.Abs(this.particles.lifetime - this.trailTime) + (this.trailTime - this.particles.lifetime)) + 2);
			if (this.particles.lifetime <= 1f)
			{
				num++;
			}
			this.referenceObjectsCache = new PlaygroundFollower[(this.cacheSize <= 0) ? (this.particles.particleCount + Mathf.CeilToInt((float)(this.particles.particleCount * num))) : this.cacheSize];
			for (int i = 0; i < this.referenceObjectsCache.Length; i++)
			{
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.referenceObject);
				this.referenceObjectsCache[i] = new PlaygroundFollower(gameObject.transform, gameObject, gameObject.GetComponent<TrailRenderer>(), 0f, 0);
				this.referenceObjectsCache[i].transform.parent = this.followerParent;
				if (this.referenceObjectsCache[i].trailRenderer != null)
				{
					this.referenceObjectsCache[i].trailRenderer.time = 0f;
				}
				this.referenceObjectsCache[i].gameObject.SetActive(false);
			}
		}

		// Token: 0x06000675 RID: 1653 RVA: 0x000269D4 File Offset: 0x00024BD4
		public virtual void OnParticleDidBirth(PlaygroundEventParticle particle)
		{
			this.waitingFollowers.Add(new PlaygroundFollower(null, null, null, (this.followerLifetime > 0f) ? this.followerLifetime : (particle.totalLifetime + this.trailTime), particle.particleId));
		}

		// Token: 0x06000676 RID: 1654 RVA: 0x00026A24 File Offset: 0x00024C24
		public virtual void OnParticleDidDie(PlaygroundEventParticle particle)
		{
			int followerWithId = this.GetFollowerWithId(particle.particleId);
			if (followerWithId < 0)
			{
				return;
			}
			this.followers[followerWithId].enabled = false;
		}

		// Token: 0x06000677 RID: 1655 RVA: 0x00026A58 File Offset: 0x00024C58
		public virtual int GetFollowerWithId(int particleId)
		{
			float num = 999f;
			int result = -1;
			for (int i = 0; i < this.followers.Count; i++)
			{
				if (this.followers[i].particleId == particleId && this.followers[i].lifetime < num)
				{
					result = i;
				}
			}
			return result;
		}

		// Token: 0x06000678 RID: 1656 RVA: 0x00026ABC File Offset: 0x00024CBC
		public virtual void Update()
		{
			if (this.waitingFollowers.Count > 0)
			{
				this.queue = this.waitingFollowers.ToArray();
			}
		}

		// Token: 0x06000679 RID: 1657 RVA: 0x00026AE0 File Offset: 0x00024CE0
		public virtual void LateUpdate()
		{
			this.UpdateFollowers();
		}

		// Token: 0x0600067A RID: 1658 RVA: 0x00026AE8 File Offset: 0x00024CE8
		public virtual void UpdateFollowers()
		{
			for (int i = 0; i < this.followers.Count; i++)
			{
				if (this.followers[i].enabled)
				{
					this.followers[i].transform.position = this.particles.particleCache[this.followers[i].particleId].position;
				}
				this.followers[i].lifetime -= Time.deltaTime;
				if (this.followers[i].lifetime <= 0f)
				{
					this.RemoveFollower(i);
				}
			}
			if (this.queue.Length > 0)
			{
				if (this.queue.Length != this.waitingFollowers.Count)
				{
					return;
				}
				int count = this.waitingFollowers.Count;
				foreach (PlaygroundFollower follower in this.queue)
				{
					this.AddFollower(follower, this.followers.Count - 1);
				}
				if (count == this.waitingFollowers.Count)
				{
					this.waitingFollowers = new List<PlaygroundFollower>();
				}
				else
				{
					this.waitingFollowers.RemoveRange(0, count - 1);
				}
				this.queue = new PlaygroundFollower[0];
			}
		}

		// Token: 0x0600067B RID: 1659 RVA: 0x00026C50 File Offset: 0x00024E50
		public virtual void AddFollower(PlaygroundFollower follower, int i)
		{
			if (follower == null)
			{
				return;
			}
			this.followers.Add(follower.Clone());
			this.followers[this.followers.Count - 1].enabled = true;
			this.followers[this.followers.Count - 1].gameObject = this.referenceObjectsCache[this.cacheIndex].gameObject;
			this.followers[this.followers.Count - 1].gameObject.SetActive(true);
			this.followers[this.followers.Count - 1].transform = this.referenceObjectsCache[this.cacheIndex].transform;
			this.followers[this.followers.Count - 1].trailRenderer = this.referenceObjectsCache[this.cacheIndex].trailRenderer;
			this.followers[this.followers.Count - 1].particleId = follower.particleId;
			this.followers[this.followers.Count - 1].transform.position = this.particles.playgroundCache.position[this.followers[this.followers.Count - 1].particleId];
			if (this.followers[this.followers.Count - 1].trailRenderer != null)
			{
				this.followers[this.followers.Count - 1].trailRenderer.time = this.trailTime;
			}
			if (this.sendEvents && this.followerEventBirth != null)
			{
				this.followerEventBirth(this.followers[this.followers.Count - 1]);
			}
			this.NextCacheIndex();
		}

		// Token: 0x0600067C RID: 1660 RVA: 0x00026E50 File Offset: 0x00025050
		public virtual void RemoveFollower(int i)
		{
			if (this.sendEvents && this.followerEventDeath != null)
			{
				this.followerEventDeath(this.followers[i]);
			}
			this.followers[i].enabled = false;
			if (this.followers[i].trailRenderer != null)
			{
				this.followers[i].trailRenderer.time = 0f;
			}
			this.followers[i].gameObject.SetActive(false);
			this.followers.RemoveAt(i);
		}

		// Token: 0x0600067D RID: 1661 RVA: 0x00026EF8 File Offset: 0x000250F8
		public virtual void NextCacheIndex()
		{
			this.cacheIndex = (this.cacheIndex + 1) % this.referenceObjectsCache.Length;
		}

		// Token: 0x0600067E RID: 1662 RVA: 0x00026F14 File Offset: 0x00025114
		public virtual PlaygroundFollower GetActiveFollower(int index)
		{
			index = Mathf.Clamp(index, 0, this.followers.Count);
			return this.followers[index];
		}

		// Token: 0x0600067F RID: 1663 RVA: 0x00026F38 File Offset: 0x00025138
		public virtual PlaygroundFollower GetCachedFollower(int index)
		{
			index = Mathf.Clamp(index, 0, this.referenceObjectsCache.Length);
			return this.referenceObjectsCache[index];
		}

		// Token: 0x06000680 RID: 1664 RVA: 0x00026F54 File Offset: 0x00025154
		public virtual int GetActiveFollowersCount()
		{
			return this.followers.Count;
		}

		// Token: 0x06000681 RID: 1665 RVA: 0x00026F64 File Offset: 0x00025164
		public virtual int GetCachedFollowersCount()
		{
			return this.referenceObjectsCache.Length;
		}

		// Token: 0x0400069C RID: 1692
		public PlaygroundParticlesC particles;

		// Token: 0x0400069D RID: 1693
		public GameObject referenceObject;

		// Token: 0x0400069E RID: 1694
		public float followerLifetime;

		// Token: 0x0400069F RID: 1695
		public int cacheSize;

		// Token: 0x040006A0 RID: 1696
		public bool sendEvents;

		// Token: 0x040006A1 RID: 1697
		protected TrailRenderer referenceTrailRenderer;

		// Token: 0x040006A2 RID: 1698
		protected float trailTime;

		// Token: 0x040006A3 RID: 1699
		protected List<PlaygroundFollower> followers = new List<PlaygroundFollower>();

		// Token: 0x040006A4 RID: 1700
		protected List<PlaygroundFollower> waitingFollowers = new List<PlaygroundFollower>();

		// Token: 0x040006A5 RID: 1701
		protected PlaygroundFollower[] referenceObjectsCache;

		// Token: 0x040006A6 RID: 1702
		protected PlaygroundFollower[] queue = new PlaygroundFollower[0];

		// Token: 0x040006A7 RID: 1703
		protected int cacheIndex;

		// Token: 0x040006A8 RID: 1704
		protected PlaygroundEventC birthEvent;

		// Token: 0x040006A9 RID: 1705
		protected PlaygroundEventC deathEvent;

		// Token: 0x040006AA RID: 1706
		protected Transform followerParent;
	}
}
